/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifRestructurarPrecioPromedio.java
 *
 * Created on 17-dic-2010, 11:12:57
 */

package GestionMovimientos;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_Cadena;
import administracion.helper.Utiles_MensajesDialogo;
import com.jidesoft.hints.ListDataIntelliHints;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import suministros.ejb30.TO.AnoTO;
import suministros.ejb30.TO.EmpresaTO;
import suministros.ejb30.TO.SuministroTO;
import administracion.delegate.GestionPedidosDelegate;
import suministros.ejb30.TO.DetalleMovimientoTO;


/**
 *
 * @author Diana
 */
public class jifRestructurarPrecioPromedio extends javax.swing.JInternalFrame {
 private ListDataIntelliHints intelliHintsSuministros;
    SuministroTO suministro;
    List<EmpresaTO> empresas;
    List<AnoTO> anomes= new ArrayList<AnoTO>();
    /** Creates new form jifRestructurarPrecioPromedio */
    public jifRestructurarPrecioPromedio() {
        initComponents();
        empresas = Listas.getEmpresas();
        this.cargaHintsIndependientes();
        this.llenarMes();
        this.jPanel3.setVisible(false);
        this.pack();
    }
  private void cargaHintsIndependientes() {
    this.intelliHintsSuministros = new ListDataIntelliHints(this.jtfDescSuministro, Listas.getSuministros());
  }
    public void llenarMes(){
        try {
            anomes = GestionPedidosDelegate.getInstance().getParametrosKardex();
        } catch (Exception ex) {
           Utiles_MensajesDialogo.mensajeAlerta("No se han registrado ingresos o salidas de suministros en la empresa");
        }
        for(int i=0; i<anomes.size(); i++){
           if(buscarAnio(anomes.get(i).getAno()))
           this.jcbAnio.addItem(anomes.get(i).getAno());
        }
         for(int i=0; i<anomes.size(); i++){
             String mes=Utiles_Cadena.getMesAnio(anomes.get(i).getMes());
             if(buscarMes(mes))
                this.jcbMes.addItem(mes);
        }

     }
   public boolean buscarAnio(String anio){
         boolean encontrado=true;
         for(int i=0; i<this.jcbAnio.getItemCount(); i++){
             if(this.jcbAnio.getItemAt(i).toString().compareTo(anio)==0){
                 encontrado=false;
             }
         }
         return encontrado;
     }
     public boolean buscarMes(String mes){
         boolean encontrado=true;
         for(int i=0; i<this.jcbMes.getItemCount(); i++){
             if(this.jcbMes.getItemAt(i).toString().compareTo(mes)==0){
                 encontrado=false;
             }
         }
         return encontrado;
     }
  private void buscarSuministros()
  {
    this.suministro = null;
    SuministroTO sumi;
    if (this.jtfCodSuministro.getText().trim().length() < 1) {

      try { sumi = (SuministroTO)this.intelliHintsSuministros.getSelectedHint();
      } catch (NullPointerException ex) {
        sumi = null;
      }
    } else {
      sumi = Listas.getSuministroCodigo(this.jtfCodSuministro.getText().trim());
    }

    if (sumi == null) {
      Utiles_MensajesDialogo.mensajeError("El suministro no existe!");
      this.jtfDescSuministro.setText("");
     } else {
      this.jtfDescSuministro.setText(sumi.toString());
      this.jtfCodSuministro.setText(sumi.getId_suministro());
      this.suministro = sumi;
    }
  }
  public boolean restructurarPrecioPromedio() throws Exception {
      boolean estado=false;
     String anio= this.jcbAnio.getSelectedItem().toString().trim();
     String mes= Utiles_Cadena.getMesAnioT(this.jcbMes.getSelectedItem().toString().trim())+"";
     String codigo=this.jtfCodSuministro.getText();
     if(this.jrbMovimiento.isSelected()){
         if(this.jtfCodSuministro.getText().compareTo("")!=0){
                try {
                    GestionPedidosDelegate.getInstance().ajustarIngresoSuministro(codigo, anio, mes);
                    estado =true;
                    Utiles_MensajesDialogo.mensajeExito();
                } catch (Exception ex) {
                     Utiles_MensajesDialogo.mensajeError("No se pudo realizar la operacion"+ex.getMessage());
                }
             
             
         }
         else{
             Utiles_MensajesDialogo.mensajeError("No se ha seleccionado ningun suministro");
         }
     }
     if(this.jrbCatalogo.isSelected()){
        List<DetalleMovimientoTO> detalles= new ArrayList<DetalleMovimientoTO>();
        detalles= GestionPedidosDelegate.getInstance().getDetallesMovimientoAjustar(anio, mes);
        if(detalles.isEmpty()){
             Utiles_MensajesDialogo.mensajeError("No se han encontrado suministros");
        }
        else{
                try {
                    GestionPedidosDelegate.getInstance().ajustarIngreso(anio, mes);
                } catch (Exception ex) {
                     Utiles_MensajesDialogo.mensajeError("No se pudo realizar la operacion"+ex.getMessage());
                }
           estado=true;
        }

     }
     return estado;
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cboBeanBindingGrupoSuministro1 = new administracion.BeanBinding.CboBeanBindingGrupoSuministro();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel3 = new javax.swing.JPanel();
        jpInversion = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jtfCodSuministro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfDescSuministro = new javax.swing.JTextField();
        jbCancelar = new javax.swing.JButton();
        jbBuscar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jrbCatalogo = new javax.swing.JRadioButton();
        jrbMovimiento = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jcbMes = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jcbAnio = new javax.swing.JComboBox();

        setTitle("AJUSTAR PRECIO PROMEDIO");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "CRITERIOS ESPECIFICOS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setText("COD.SUMINISTRO");

        jtfCodSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfCodSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodSuministroActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setText("DESC.SUMINISTRO");

        jtfDescSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfDescSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDescSuministroActionPerformed(evt);
            }
        });
        jtfDescSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescSuministroKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jpInversionLayout = new javax.swing.GroupLayout(jpInversion);
        jpInversion.setLayout(jpInversionLayout);
        jpInversionLayout.setHorizontalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpInversionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jpInversionLayout.setVerticalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel9)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel10)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CANCELAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        jbBuscar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/apply.png"))); // NOI18N
        jbBuscar.setMnemonic('B');
        jbBuscar.setText("ACEPTAR");
        jbBuscar.setToolTipText("Cancelar la Operacion");
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });
        jbBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbBuscarKeyPressed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "TIPO DE REPORTE", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(0, 0, 255))); // NOI18N

        buttonGroup1.add(jrbCatalogo);
        jrbCatalogo.setText("AJUSTAR PRECIO DE TODOS LOS SUMINISTROS");
        jrbCatalogo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbCatalogoActionPerformed(evt);
            }
        });

        buttonGroup1.add(jrbMovimiento);
        jrbMovimiento.setText("AJUSTAR PRECIO POR SUMINISTRO");
        jrbMovimiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMovimientoActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("MES:");

        jcbMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbMesKeyPressed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setText("AÑO:");

        jcbAnio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbAnioKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(10, 10, 10)
                        .addComponent(jcbMes, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(34, 34, 34)
                        .addComponent(jcbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jrbCatalogo)
                    .addComponent(jrbMovimiento))
                .addContainerGap(279, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jrbCatalogo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jrbMovimiento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jPanel3, 0, 602, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addComponent(jbBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbBuscar)
                    .addComponent(jbCancelar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfCodSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodSuministroActionPerformed

        if (this.jtfCodSuministro.getText().trim().length() < 1)
            this.jtfDescSuministro.requestFocus();
        else
            buscarSuministros();
}//GEN-LAST:event_jtfCodSuministroActionPerformed

    private void jtfDescSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDescSuministroActionPerformed

        buscarSuministros();
}//GEN-LAST:event_jtfDescSuministroActionPerformed

    private void jtfDescSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescSuministroKeyPressed

        if (this.jtfCodSuministro.getText().trim().length() > 0)
            this.jtfCodSuministro.setText("");
}//GEN-LAST:event_jtfDescSuministroKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

}//GEN-LAST:event_jbCancelarKeyPressed

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
        try {
            if (this.restructurarPrecioPromedio()) {
                Utiles_MensajesDialogo.mensajeExito();
            }
            else{
                Utiles_MensajesDialogo.mensajeError("No se pudo realizar la operacion");
            }
        } catch (Exception ex) {
            Logger.getLogger(jifRestructurarPrecioPromedio.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jbBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbBuscarKeyPressed

    }//GEN-LAST:event_jbBuscarKeyPressed

    private void jcbMesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbMesKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jcbAnio.requestFocus();
        }
}//GEN-LAST:event_jcbMesKeyPressed

    private void jcbAnioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbAnioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
        }
}//GEN-LAST:event_jcbAnioKeyPressed

    private void jrbCatalogoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbCatalogoActionPerformed
      if(this.jrbCatalogo.isSelected()){
          this.jPanel3.setVisible(false);
      }
      else{
          this.jPanel3.setVisible(true);
      }
      this.pack();
    }//GEN-LAST:event_jrbCatalogoActionPerformed

    private void jrbMovimientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMovimientoActionPerformed
       if(this.jrbMovimiento.isSelected()){
          this.jPanel3.setVisible(true);
      }
      else{
          this.jPanel3.setVisible(false);
      }
       this.pack();
    }//GEN-LAST:event_jrbMovimientoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private administracion.BeanBinding.CboBeanBindingGrupoSuministro cboBeanBindingGrupoSuministro1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JComboBox jcbAnio;
    private javax.swing.JComboBox jcbMes;
    private javax.swing.JPanel jpInversion;
    private javax.swing.JRadioButton jrbCatalogo;
    private javax.swing.JRadioButton jrbMovimiento;
    private javax.swing.JTextField jtfCodSuministro;
    private javax.swing.JTextField jtfDescSuministro;
    // End of variables declaration//GEN-END:variables

}
